package com.zhengsr.latte.ec.ec.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.zhengsr.cusbottomlib.IBottomClickListener;
import com.zhengsr.cusbottomlib.view.CusBottomLayout;
import com.zhengsr.latte.delegates.LatteDelegate;
import com.zhengsr.latte.ec.R;
import com.zhengsr.latte.ec.R2;
import com.zhengsr.latte.ec.ec.main.fragment.FindFragment;
import com.zhengsr.latte.ec.ec.main.fragment.HomeFragment;
import com.zhengsr.latte.ec.ec.main.fragment.MineFragment;
import com.zhengsr.latte.ec.ec.main.fragment.ShopFragment;
import com.zhengsr.latte.ec.ec.main.fragment.SortFragment;

import butterknife.BindView;

public class BottomDelagete extends LatteDelegate implements IBottomClickListener {

    @BindView(R2.id.bottom_ly)
    CusBottomLayout mCusBottomLayout;



    private HomeFragment mHomeFragment;
    private SortFragment mSortFragment;
    private FindFragment mFindFragment;
    private ShopFragment mShopFragment;
    private MineFragment mMineFragment;

    @Override
    public Object getLayout() {
        return R.layout.activity_bottom;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState, View view) {
        super.initView(savedInstanceState, view);
        mCusBottomLayout.setBottomClickListener(this);
        initData();

    }

    private void initData() {
        mHomeFragment = HomeFragment.getInstance(this);
        getFragmentManager().beginTransaction().add(R.id.bottom_content,mHomeFragment).commit();


    }

    @Override
    public void onClick(View view, int position) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        resetFragment(ft);
        switch (position){
            case 0:
                if (mHomeFragment == null){
                    mHomeFragment = HomeFragment.getInstance(this);
                    ft.add(R.id.bottom_content,mHomeFragment);
                }else{
                    ft.show(mHomeFragment);
                }
                break;
            case 1:
                if (mSortFragment == null){
                    mSortFragment = new SortFragment();
                    ft.add(R.id.bottom_content,mSortFragment);
                }else{
                    ft.show(mSortFragment);
                }
                break;
            case 2:
                if (mFindFragment == null){
                    mFindFragment = new FindFragment();
                    ft.add(R.id.bottom_content,mFindFragment);
                }else{
                    ft.show(mFindFragment);
                }
                break;
            case 3:
                if (mShopFragment == null){
                    mShopFragment = new ShopFragment();
                    ft.add(R.id.bottom_content,mShopFragment);
                }else{
                    ft.show(mSortFragment);
                }
                break;
            case 4:
                if (mMineFragment == null){
                    mMineFragment = new MineFragment();
                    ft.add(R.id.bottom_content,mMineFragment);
                }else{
                    ft.show(mMineFragment);
                }
                break;
            default:
                break;
        }
        ft.commit();
    }



    private void resetFragment(FragmentTransaction ft){
        if (mHomeFragment != null){
            ft.hide(mHomeFragment);
        }
        if (mSortFragment != null){
            ft.hide(mSortFragment);
        }
        if (mFindFragment != null){
            ft.hide(mFindFragment);
        }
        if (mShopFragment != null){
            ft.hide(mShopFragment);
        }

        if (mMineFragment != null){
            ft.hide(mMineFragment);
        }
    }

}
